home *** CD-ROM | disk | FTP | other *** search
/ PC Elektro 3 / PC-Elektro-3-cd1.bin / KBan 2.0 / KBANSRC.LZH / SRC / PROG / XY.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-18  |  1.2 KB  |  65 lines

  1. /*
  2.  * the class XY
  3.  * Copyright (C) 1996, 1997 Kazutaka Hirata <khirata@jove.acs.unt.edu>
  4.  */
  5.  
  6. #include <math.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. #include "record.h"
  11.  
  12. #include "xy.h"
  13.  
  14. int compare_distance_lt(const XY& p, const XY& q, XYT d)
  15. {
  16.   XYT dx = p.x() - q.x();
  17.   XYT dy = p.y() - q.y();
  18.   if(dx < 0) {
  19.     dx = - dx;
  20.   }
  21.   if(dy < 0) {
  22.     dy = - dy;
  23.   }
  24.   if((d < dx) || (d < dy)) {
  25.     return false;
  26.   }
  27.   return (dx * dx + dy * dy) < d * d;
  28. }
  29.  
  30. double XY::get_radian(const XY& p) const {
  31.   double r = get_distance(p, *this);
  32.   double rate = (p.m_x - m_x) / r;
  33.   if(1.0 < rate) {
  34.     rate = 1.0;
  35.   }
  36.   if(rate < -1.0) {
  37.     rate = -1.0;
  38.   }
  39.   double radian = acos(rate);
  40.  
  41.   if(p.m_y < m_y) {
  42.     radian = M_PI * 2 - radian;
  43.   }
  44.   return radian;
  45. }
  46.  
  47. void XY::rotate(const XY& src, double rad)
  48. {
  49.   const XY tmp = src;
  50.   double c = cos(rad);
  51.   double s = sin(rad);
  52.   set(
  53.     XYT(tmp.x() * c - tmp.y() * s),
  54.     XYT(tmp.x() * s + tmp.y() * c)
  55.   );
  56. }
  57.  
  58. bool XY::is_in_box(const XY& p, const XY& q) const
  59. {
  60.   const XY r = get_min(p, q);
  61.   const XY s = get_max(p, q);
  62.   return (r.x() <= x() && x() <= s.x()
  63.        && r.y() <= y() && y() <= s.y());
  64. }
  65.